package com.pan.service.impl;

import com.pan.dao.StockDao;
import com.pan.dao.StockSelectDao;
import com.pan.dao.StockUpdateDao;
import com.pan.entity.Result;
import com.pan.entity.Stock;
import com.pan.enume.ResultEnum;
import com.pan.service.OperationStockHistoryService;
import com.pan.service.StockService;
import com.pan.tools.ConvertObject;
import com.pan.tools.RedisTools;
import com.pan.vo.OperationStockHistoryInsertVo;
import com.pan.vo.StockInsertVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;
import java.util.stream.Collectors;

@Service("stockService")
public class StockServiceImpl implements StockService {


    @Resource
    private StockDao stockDao;

    @Resource
    private StockUpdateDao stockUpdateDao;

    @Resource
    private StockSelectDao stockSelectDao;

    @Resource
    private OperationStockHistoryService operationStockHistoryService;

    @Transactional
    @Override
    public Result<Long> insert(String adminTicket, StockInsertVo stockInsertVo){
        Integer storageDay = stockInsertVo.getStorageDay();
        Long nowTime = System.currentTimeMillis();
        Long expirationTime = nowTime + (storageDay*60*60*1000*24);
        Stock insertStock = (Stock) ConvertObject.convertObject(Stock.class,stockInsertVo);
        insertStock.setInsertTime(new Timestamp(nowTime));
        insertStock.setStockStatus(0);
        insertStock.setExpirationTime(new Timestamp(expirationTime));
        Long id = stockDao.insert(insertStock);
        OperationStockHistoryInsertVo operationStockHistoryInsertVo = new OperationStockHistoryInsertVo();
        operationStockHistoryInsertVo.setStockId(id)
                        .setGoodsCount(stockInsertVo.getGoodsCount())
                        .setOperation("add");
        operationStockHistoryService.insert(adminTicket,operationStockHistoryInsertVo);
        Result<Long> result = new Result<>(ResultEnum.SUCCESS,id);
        return result;
    }

    @Transactional
    @Override
    public Result updateReduceStock(String adminTicket, Long id, Long reduceCounts){
        Stock stock = stockSelectDao.selectById(id);
        if (stock==null){
            return new Result(ResultEnum.NOT_FOUND_ERROR);
        }
        Long goodsCount = stock.getGoodsCount();
        if (goodsCount<reduceCounts){
            return new Result(403,"库存不足");
        }
        Long nowCount = goodsCount-reduceCounts;
        stockUpdateDao.updateGoodsCount(id,nowCount);
        String operationType = "reduce";
        if (nowCount.equals(0L)){
            stockUpdateDao.updateStockStatus(id,1);
            operationType = "delete";
        }
        OperationStockHistoryInsertVo operationStockHistoryInsertVo = new OperationStockHistoryInsertVo();
        operationStockHistoryInsertVo.setStockId(id)
                        .setOperation(operationType)
                        .setGoodsCount(nowCount);
        operationStockHistoryService.insert(adminTicket, operationStockHistoryInsertVo);
        return new Result(ResultEnum.SUCCESS);
    }

    public Result updateStatus(String adminTicket,Long id){
        Stock stock = stockSelectDao.selectById(id);
        if (stock==null){
            return new Result(ResultEnum.NOT_FOUND_ERROR);
        }
        stockUpdateDao.updateStockStatus(id,1);
        return new Result(ResultEnum.SUCCESS);
    }

    @Override
    public Result<Stock> selectById(String adminTicket, Long id){
        Stock stock = stockSelectDao.selectById(id);
        if (stock == null){
            return new Result<>(ResultEnum.NOT_FOUND_ERROR);
        }
        return new Result<>(ResultEnum.SUCCESS,stock);
    }

    @Override
    public Result<List<Stock>> selectAllByGoodsId(String adminTicket, Long goodsId){
        List<Stock> li = stockSelectDao.selectByGoodsId(goodsId);
        if (li == null){
            return new Result<>(ResultEnum.NOT_FOUND_ERROR);
        }
        return new Result<>(ResultEnum.SUCCESS,li);
    }

    @Override
    public Result<List<Stock>> selectByGoodsId(String adminTicket, Long goodsId){
        List<Stock> li = stockSelectDao.selectByGoodsId(goodsId);
        if (li == null){
            return new Result<>(ResultEnum.NOT_FOUND_ERROR);
        }
        li = li.stream().filter(i->i.getStockStatus()!=1).collect(Collectors.toList());
        return new Result<>(ResultEnum.SUCCESS,li);
    }


    @Override
    public Result<Long> getGoodsCount(Long goodsId){
        List<Stock> li = stockSelectDao.selectByGoodsId(goodsId);
        if (li == null||li.size()==0){
            return new Result<>(ResultEnum.NOT_FOUND_ERROR);
        }
        Long count = li.stream().filter(i->{
            if (i.getStockStatus()==1){
                return false;
            }
            else {
                return true;
            }
        }).mapToLong(Stock::getGoodsCount).reduce(0L,(x,y)->x+y);
        return new Result<>(ResultEnum.SUCCESS,count);
    }



}
